package com.orangelife.mobile.individual.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.util.IntentHelper;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.individual.fragment.ExpressResultFrament;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.widget.EditText.ClearEditText;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 快递查询
 * Created by YangWei
 * on 2016/3/14.
 */
public class ExpressQueryActivity extends OrangeLifeBaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvExpressCompany)
    TextView tvExpressCompany;
    @Bind(R.id.rlExpress)
    RelativeLayout rlExpress;
    @Bind(R.id.etExpressNumber)
    ClearEditText etExpressNumber;
    @Bind(R.id.btnQuery)
    Button btnQuery;
    @Bind(R.id.flResult)
    FrameLayout flResult;
    private String expNo;//快递单号
    private String expCode;//快递公司编号
    private String expName;//快递公司
    private ExpressResultFrament frament;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_express_query);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        tvTitle.setText("快递查询");
        rlExpress.setOnClickListener(this);
        btnQuery.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnQuery:
                expNo = String.valueOf(etExpressNumber.getText());
                if (StringUtil.isBlank(expNo)) {
                    ToastHelper.getInstance().displayToastWithQuickClose("请输入快递单号");
                    return;
                }
                if (StringUtil.isBlank(expCode)) {
                    expCode="";
                }
                initFragment();
                break;
            case R.id.rlExpress:
                IntentHelper.getInstances()
                        .bundleWithBundleForResult(
                                ExpressQueryActivity.this,
                                ExpressCompanyActivity.class,
                                new Bundle(),
                                10);
                break;
        }
    }



    private void initFragment() {
        FragmentManager fm = getSupportFragmentManager();
        // 开启Fragment事务
        FragmentTransaction transaction = fm.beginTransaction();
        if (frament == null) {
            frament = new ExpressResultFrament();
            transaction.replace(R.id.flResult, frament);
            transaction.commit();
        }
        frament.getSearchResutl(expNo,expCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10 && resultCode == RESULT_OK) {
            expName = data.getStringExtra("expName");
            expCode = data.getStringExtra("expCode");
            tvExpressCompany.setText(expName);
        }
    }
}
